#include "stdafx.h"
#ifdef LIT_OGRE_SUPPORT
#include "LitPhysicsShapes.h"

namespace Lit
{
	namespace Physics
	{
		CollisionShape::CollisionShape( const Matrix & initial, ShapeType type, int id ) : mType(type), mId(id), mInitialMatrix( initial ){}
		CollisionShape::~CollisionShape(){}

		int CollisionShape::shapeId() const{ return mId; }
		const Matrix& CollisionShape::initialMatrix() const{ return mInitialMatrix; }
		ShapeType CollisionShape::shapeType() const{ return mType; }
	
		InertialMatrix CollisionShape::calculateInertialMatrix( float mass, _CollisionPtr col ) const{ 
			InertialMatrix mat(mass);
			NewtonConvexCollisionCalculateInertialMatrix (*col, &mat.inertia.x, &mat.origin.x);	
			return mat;
		}
		void CollisionShape::destroyShape( _WorldPtr world, _CollisionPtr col )
		{
			NewtonReleaseCollision( *world, *col );
		}

		/*********************
		/*		NULL
		/*********************/
		NullShape::NullShape(  ) : CollisionShape( Matrix(), ST_NULL, 0 ){}
		NullShape::~NullShape() {}

		_CollisionPtr NullShape::createShape( _WorldPtr world ){return NewtonCreateNull( *world );	}
	
		/*********************
		/*		PLANE
		/*********************/
		PlaneShape::PlaneShape( const Lit::AABB& box, int shapeId, const Matrix & transfo ) : mBox( box ), CollisionShape( transfo, ST_PLANE, shapeId ){}
		PlaneShape::~PlaneShape() {}

		_CollisionPtr PlaneShape::createShape( _WorldPtr world ){
			Lit::Vec3 origin (mBox.getCenter());
			Matrix offset(/*origin+*/initialMatrix().getPosition(), initialMatrix().getOrientation());
			Lit::Vec3 size (mBox.getSize());
			return NewtonCreateBox (*world, size.x, size.y+1.0, size.z, shapeId(), offset.toNewton());
		}
		/*********************
		/*		BOX
		/*********************/
		BoxShape::BoxShape( const Lit::AABB& box, int shapeId, const Matrix & transfo ) : mBox( box ), CollisionShape( transfo, ST_BOX, shapeId ){}
		BoxShape::~BoxShape() {}

		_CollisionPtr BoxShape::createShape( _WorldPtr world )
		{
			Lit::Vec3 origin (mBox.getCenter());
			Matrix offset(/*origin+*/initialMatrix().getPosition(), initialMatrix().getOrientation());

			Lit::Vec3 size (mBox.getSize());
			return NewtonCreateBox (*world, size.x, size.y, size.z, shapeId(), offset.toNewton());
		}
		/*********************
		/*		SPHERE
		/*********************/
		SphereShape::SphereShape( const Lit::AABB& box, int shapeId, const Matrix & transfo ) : mAABB( box ), CollisionShape( transfo, ST_SPHERE, shapeId ){}
		SphereShape::~SphereShape() {}

		_CollisionPtr SphereShape::createShape( _WorldPtr world )
		{
			Lit::Vec3 origin (mAABB.getCenter());
			Matrix offset(/*origin+*/initialMatrix().getPosition(), initialMatrix().getOrientation());

			return NewtonCreateSphere (*world, mAABB.getHalfSize().x, mAABB.getHalfSize().y, mAABB.getHalfSize().z, shapeId(), offset.toNewton());
		}
	};
};

#endif